package com.kaoyaya.view;

import java.util.regex.Pattern;

import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.data.entity.Client;
import com.kaoyaya.delivery.fragment.CartFmt;
import com.kaoyaya.util.StringUtil;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * 自定义的dialog
 * @date 2013-5-27
 * @author Goven
 * @email gxl3999@gmail.com
 */
public class ClientInfoDialog extends Dialog {

	Context context;
	LayoutInflater inflater;
	Button btnOk, btnCancel;
	CartFmt cartFmt;
	EditText etMobile, etEmail, etName, etWang, etQQ, etUsername, etMemo;

	public ClientInfoDialog(Context context) {
		super(context);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public ClientInfoDialog(Context context, int theme, CartFmt cartFmt) {
		super(context, theme);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.cartFmt = cartFmt;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View mView = inflater.inflate(R.layout.dialog_client_info, null);
		this.setContentView(mView);
		this.etMobile = (EditText) mView.findViewById(R.id.etMobile);
		this.etEmail = (EditText) mView.findViewById(R.id.etEmail);
		this.etName = (EditText) mView.findViewById(R.id.etName);
		this.etWang = (EditText) mView.findViewById(R.id.etWang);
		this.etQQ = (EditText) mView.findViewById(R.id.etQQ);
		this.etUsername = (EditText) mView.findViewById(R.id.etUsername);
		this.etMemo = (EditText) mView.findViewById(R.id.etMemo);
		this.btnOk = (Button) mView.findViewById(R.id.btnOK);
		this.btnCancel = (Button) mView.findViewById(R.id.btnCancel);
		btnOk.setOnClickListener(clickListener);
		btnCancel.setOnClickListener(clickListener);
	}
	
	boolean validate(Client client) {
		boolean result = true;
		if (StringUtil.isTrimBlank(client.getMobile()) || client.getMobile().length() >11) {
			etMobile.setError("必须输入11位手机号！");
			result = false;
		}
		if (!validateEmail(client.getEmail())) {
			etEmail.setError("请输入正确格式的电子邮箱！");
			result = false;
		}
		/*if (StringUtil.isTrimBlank(client.getWang()) && StringUtil.isTrimBlank(client.getQq())) {
			etWang.setError("必须输入旺旺号或QQ号");
			result = false;
		}*/
		return result;
	}
	
	public boolean validateEmail(String email) {
		boolean result = true;
		if (StringUtil.isTrimBlank(email)) {
			result = false;
		} else {
			result = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*").matcher(email).find();
		}
		return result;
	}
	
	View.OnClickListener clickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnOK:
				String mobile = etMobile.getText().toString();
				String email = etEmail.getText().toString();
				String name = etName.getText().toString();
				String wang = etWang.getText().toString();
				String username = etUsername.getText().toString();
				String memo = etMemo.getText().toString();
				String qq = etQQ.getText().toString();
				Client client = new Client();
				client.setEmail(email);
				client.setMemo(memo);
				client.setMobile(mobile);
				client.setRealName(name);
				client.setUsername(username);
				client.setWang(wang);
				client.setQq(qq);
				if (validate(client)) {
					dismiss();
					cartFmt.ensureDelivery(client);
				}
				break;
			case R.id.btnCancel:
				dismiss();
				break;
			}
		}
	};

}
